﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using YiShop.Framework.Cache.Options;

namespace YiShop.Framework.Cache
{
    public static class CacheExtensions
    {
        public static IServiceCollection AddCache(this IServiceCollection services, IConfiguration configuration)
        {
            var cacheConfig = configuration.GetSection("CacheConfig").Get<CacheSettings>() ?? throw new Exception("未正确配置缓存");
            if (cacheConfig.CacheType == CacheType.Memory)
            {
                services.AddTransient<ICache, MemoryCache>();
                return AddMemoryCache(services);
            }
            else
            {
                services.AddTransient<ICache, RedisCache>();
                services.Configure<RedisSettings>(configuration.GetSection("CacheConfig:RedisConfig"));
                return AddStackExchangeRedisCache(services, cacheConfig.RedisConfig);
            }
        }

        private static IServiceCollection AddMemoryCache(IServiceCollection services)
        {
            services.AddDistributedMemoryCache();
            return services;
        }

        private static IServiceCollection AddStackExchangeRedisCache(IServiceCollection services, RedisSettings? redisConfig)
        {
            if (redisConfig == null)
                throw new Exception("redis配置有误");
            services.AddStackExchangeRedisCache(options =>
            {
                // 连接字符串
                options.Configuration = $"{redisConfig.Host}:{redisConfig.Port},defaultDatabase={redisConfig.Database}, password={redisConfig.Password}";
                // 键名前缀
                options.InstanceName = redisConfig.InstanceName;

                //options.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions
                //{
                //    DefaultDatabase = redisConfig.Database,
                //    Password = redisConfig.Password,
                //    EndPoints = { $"{redisConfig.Host}:{redisConfig.Port}" }
                //    //ConnectTimeout = 5000,//设置建立连接到Redis服务器的超时时间为5000毫秒
                //    //SyncTimeout = 5000,//设置对Redis服务器进行同步操作的超时时间为5000毫秒
                //    //Ssl = false,//设置启用SSL安全加密传输Redis数据
                //    //ConnectRetry = 3 // 重试次数
                //};
            });

            // IDistributedCache 没有 模糊匹配 key 的方法, 此处注入 IConnectionMultiplexer 来 实现 IDistributedCache 中不存在的功能
            services.AddSingleton<IConnectionMultiplexer>(sp =>
            //ConnectionMultiplexer.Connect(new ConfigurationOptions
            //{
            //    EndPoints = { $"{redisConfig.Host}:{redisConfig.Port}" },
            //    DefaultDatabase = redisConfig.Database,
            //    Password = redisConfig.Password,
            //    AbortOnConnectFail = false
            //})
                     {
                         var config = ConfigurationOptions.Parse("localhost:6379,allowAdmin=true");
                         return ConnectionMultiplexer.Connect(config);
                     }
            );

            return services;
        }
    }
}
